package br.ufrj.ppgi.greco.controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.ufrj.ppgi.greco.model.dao.DAOObjectProperty;
import br.ufrj.ppgi.greco.model.dao.DAOProperty;
import br.ufrj.ppgi.greco.model.dao.DAOResource;
import br.ufrj.ppgi.greco.model.entity.EntityObjectProperty;
import br.ufrj.ppgi.greco.model.entity.EntityResource;

@Resource
public class ObjectPropertyController
{

	private DAOResource dr;
	private DAOProperty dp;
	private DAOObjectProperty dop;
	private Result result;
	
	public ObjectPropertyController(DAOResource dr, DAOObjectProperty dop, Result result, DAOProperty dp)
	{
		this.dr = dr;
		this.dp = dp;
		this.dop = dop;
		this.result = result;
	}
	
	@Get
	@Path("/config/resources/{id}/objectProperties/new")
	public void newObjectProperty(int id)
	{
		this.result.include("id",id);
		this.result.include("resources",this.dr.getAllResourcesJson());
		this.result.include("properties",this.dp.getAllPropertiesJson());
	}
	
	@Post
	@Path("/config/resources/{id}/objectProperties/create")
	public void createObjectProperty(int id,String predicate, String objectUri, Boolean isDominant)
	{
		EntityResource resource = this.dr.getByID(id);
		
		EntityObjectProperty objProp = new EntityObjectProperty();
		objProp.setResource(resource);
		objProp.setPredicate(predicate);
		objProp.setObjectUri(objectUri);
		objProp.setIsDominant(isDominant);
		
		this.dop.insert(objProp);
		this.result.redirectTo(ResourceController.class).listResource(id);
	}
	
	
	@Get
	@Path("/config/resources/{id}/objectProperties/{idObjectProperty}")
	public void listObjectProperty(int id, int idObjectProperty)
	{
		this.result.include("id",id);
		this.result.include("eop",this.dop.getByID(idObjectProperty));
		
		this.result.include("resources",this.dr.getAllResourcesJson());
		this.result.include("properties",this.dp.getAllPropertiesJson());

	}
	
	@Post
	@Path("/config/resources/{id}/objectProperties/update")
	public void updateObjectProperty(int id, EntityObjectProperty eop)
	{
		EntityResource resource = this.dr.getByID(id);
		eop.setResource(resource);
		
		this.dop.update(eop);	
		this.result.redirectTo(ResourceController.class).listResource(id);
	}
	
	@Get
	@Path("/config/resources/{resourceId}/objectProperties/{idObjectProperty}/delete")
	public void deleteObjectProperty(int resourceId, int idObjectProperty)
	{
		this.dop.deleteByID(idObjectProperty);
		this.result.redirectTo(ResourceController.class).listResource(resourceId);
	}
	
}
